package com.wjw.service;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.wjw.config.OSSConfig;
import com.wjw.utils.OSSUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Transactional
public class ImageUpload {

    @Autowired
    private OSSUtil ossUtil;

    @Autowired
    private OSSConfig ossConfig;

    public String img(MultipartFile productImg) {
        String originalFilename = productImg.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        String fileName = "img-product/" + IdWorker.get32UUID() + "." + extension;
        try {
            ossUtil.uploadByBytes(fileName, productImg.getBytes());
			// 真实访问地址
            return ossConfig.getImagePrefix() + fileName;
        } catch (IOException e) {
            return "";
        }
    }
}